//
//  FacilityDisplayMain.m
//  YOGProj
//
//  Created by liyun on 14-6-26.
//  Copyright (c) 2014年 liyun. All rights reserved.
//

#import "FacilityDisplayMain.h"
#import "SidebarViewController.h"
#import "MacroDefinition.h"
#import "DownImageOperation.h"
#import "FacilityDisplayCustomCell.h"
#import "FacilityDetailInfo.h"
#import "SendHttpOperation.h"


@interface FacilityDisplayMain ()<UITableViewDataSource,UITableViewDelegate,VenuesListProtocol>
{
    IBOutlet UITableView *facilityDisplayTable;
    NSArray *titleArr;
    NSArray *imageNameArr;
    NSDictionary *faclityCodeDic;
}

@end

@implementation FacilityDisplayMain

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)navigationShowLeft
{
    [[SidebarViewController share] showLeftView];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
    
    if(titleArr.count == 0)
    {
        [[SendHttpOperation shareSendHttpRequest] getVenuesList];
        [SendHttpOperation shareSendHttpRequest].venuesListDelegate = self;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"title2", @"");
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:MAIN_MENU_BUTTON_IMAGE] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:MAIN_MENU_SELECT_IMAGE] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(navigationShowLeft) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItemleft=[[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem=barItemleft;
    
    facilityDisplayTable.separatorStyle = UITableViewCellSelectionStyleNone;
    
    NSArray *languages = [NSLocale preferredLanguages];
    NSString *currentLanguage = [languages objectAtIndex:0];
    
    if([currentLanguage isEqualToString:@"zh-Hans"])
    {
        faclityCodeDic = @{@"龙江体育馆":@{@"龙江体育馆":@"LJG"},
                           @"南京市水上运动学校":@{@"南京市水上运动学校":@"XRC"},
                           @"玄武湖铁人三项赛场":@{@"玄武湖铁人三项赛场":@"XWT"},
                           @"五台山":@{@"五台山篮球场":@"WTB",@"五台山体育馆":@"WTG",@"五台山体育场":@"WTS"},
                           @"江宁体育中心":@{@"江宁体育中心体育馆":@"JNG",@"江宁体育中心体育场":@"JNS"},
                           @"南京国际博览中心":@{@"南京国际博览中心D馆":@"IED",@"南京国际博览中心E馆":@"IEE",@"南京国际博览中心F馆":@"IEF"},
                           @"南京奥体中心":@{@"南京奥体中心游泳馆":@"OSN",@"南京奥体中心体育场":@"OSS",@"南京奥体中心体育馆":@"OSG"},
                           @"青奥体育公园":@{@"青奥体育公园沙滩排球赛场":@"SPB",@"青奥体育公园曲棍球场":@"SPH",@"青奥体育公园橄榄球场":@"SPR"}};
    }
    else
    {
        faclityCodeDic = @{@"Longjiang Gymnasium":@{@"Longjiang Gymnasium":@"LJG"},
                           @"Nanjing Rowing-Canoeing School":@{@"Nanjing Rowing-Canoeing School":@"XRC"},
                           @"Xuanwu Lake Triathlon Venue":@{@"Xuanwu Lake Triathlon Venue":@"XWT"},
                           @"Wutaishan":@{@"Wutaishan Basketball Venue":@"WTB",@"Wutaishan Gymnasium":@"WTG",@"Wutaishan Stadium":@"WTS"},
                           @"Jiangning Sports Centre":@{@"Jiangning Sports Centre Gymnasium":@"JNG",@"Jiangning Sports Centre Stadium":@"JNS"},
                           @"Nanjing International Expo Centre":@{@"Nanjing International Expo Centre Hall D":@"IED",@"Nanjing International Expo Centre Hall E":@"IEE",@"Nanjing International Expo Centre Hall F":@"IEF"},
                           @"Nanjing Olympic Sports Centre":@{@"Nanjing Olympic Sports Centre Natatorium":@"OSN",@"Nanjing Olympic Sports Centre Stadium":@"OSS",@"Nanjing Olympic Sports Centre Gymnasium":@"OSG"},
                           @"Youth Olympic Sports Park":@{@"Nanjing Youth Olympic Sports Park Beach Volleyball Venue":@"SPB",@"Nanjing Youth Olympic Sports Park Hockey Field":@"SPH",@"Nanjing Youth Olympic Sports Park Rugby Field":@"SPR"}};
    }
}

- (void)passVenuesList:(NSDictionary *)paramDic
{
    titleArr = [paramDic objectForKey:@"retData"];
    
    NSLog(@"titleArr-->%@",titleArr);
    [facilityDisplayTable reloadData];
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int  count= titleArr.count%2==0?0:1;
    return titleArr.count/2+count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FacilityDisplayCustomCell* cell = (FacilityDisplayCustomCell*)[tableView dequeueReusableCellWithIdentifier:@"FacilityDisplayCustomCell"];
    if (cell == nil)
    {
        NSArray* arry = [[NSBundle mainBundle] loadNibNamed:@"FacilityDisplayCustomCell" owner:nil options:nil];
        cell = [arry objectAtIndex:0];
    }
    
//    NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
//    for (UIView *subview in subviews)
//    {
//        [subview removeFromSuperview];
//    }
    
//    NSString *urlStr = [tempDic objectForKey:@"InfoPicios"];
//    if(![urlStr isEqualToString:@""])
//    {
//        if(urlStr.length >26)
//        {
//            DownImageOperation *diOperation=[[DownImageOperation alloc]
//                                             initWithUrl:urlStr imageView:icoImage];
//            [m_operationQuene addOperation:diOperation];
//        }
//        else
//        {
//            icoImage.image = [UIImage imageNamed:@"p21.png"];
//        }
//    }
//    else
//    {
//        icoImage.image = [UIImage imageNamed:@"p21.png"];
//    }
        
    NSString *imageName1 = [NSString stringWithFormat:@"%@.jpg",[titleArr[indexPath.row*2] objectForKey:@"venuesCode"]];
    cell.imageView1.image = [UIImage imageNamed:imageName1];
    cell.label1.text = [titleArr[indexPath.row*2] objectForKey:@"name"];
    
    if(titleArr.count > indexPath.row*2+1)
    {
        NSString *imageName2 = [NSString stringWithFormat:@"%@.jpg",[titleArr[indexPath.row*2+1] objectForKey:@"venuesCode"]];
        cell.imageView2.image = [UIImage imageNamed:imageName2];
        cell.label2.text      = [titleArr[indexPath.row*2+1] objectForKey:@"name"];
        
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        button2.frame = cell.imageView2.frame;
        button2.tag = indexPath.row*2+1;
        [button2 addTarget:self action:@selector(facilityInfo:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button2];
    }
    else
    {
        cell.imageView2.hidden = YES;
        cell.imageView3.hidden = YES;
        cell.label2.hidden     = YES;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self pushToDetailInfo:titleArr[indexPath.row*2] title:[titleArr[indexPath.row*2] objectForKey:@"name"]];
}

- (void)facilityInfo:(UIButton *)button
{
    [self pushToDetailInfo:titleArr[button.tag] title:[titleArr[button.tag] objectForKey:@"name"]];
}

- (void)pushToDetailInfo:(NSDictionary *)paramDic
                   title:(NSString *)titleStr
{
    FacilityDetailInfo *instance = [[FacilityDetailInfo alloc] init];
    instance.title = titleStr;
    instance.detailInfoDic = paramDic;
    if([[faclityCodeDic allKeys] containsObject:titleStr])
        instance.faclityCodeDic = faclityCodeDic[titleStr];
    else
        instance.faclityCodeDic = @{};
    [self.navigationController pushViewController:instance animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
